﻿
using Autofac.Core;
using DomainService.IService;
using DomainService.Service;
using Infrastructure.IRepository;
using Model.OverallAtuhModel;
using System;
using Utility;

namespace ApprovalFlow_WebApi.PlugIn
{
    /// <summary>
    /// 过期插件
    /// </summary>
    public class ExpirePlugin : BackgroundService
    {
        /// <summary>
        /// 实例化异常日志服务
        /// </summary>
        private readonly IFlowMessageService _flowMessageService;


        /// <summary>
        /// 构造实列化
        /// </summary>
        /// <param name="flowMessageService"></param>
        public ExpirePlugin(IFlowMessageService flowMessageService)
        {
            _flowMessageService = flowMessageService;
        }

        /// <summary>
        /// 定时任务检查
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                var userData = MemoryCacheHelper.GetCachedDataByKey<List<SysUser>>(ConstHelper.UserCacheKey);
                if (userData.success == true)
                    _flowMessageService.CheckExpireFlowNode();
                // 每10分钟检查1次
                await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);

                //没10秒检查一次
                //await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
            }
        }
    }
}
